REFER = hg18.di.fa
HAPMAP = hapmap.NA19238.txt
ISSAM = -sam 
PLANB = 
MINTOTAL =1
EXON =-exon
#EXON =

#REFER = mm9.diploid.strip.fa
TARGETS = alnSNPsInfo  generateReads maskSNP stripDiploid haploid2diploid readsAllele unstripSam

P1 = GSM453868_argonne_090312_HGAC_S100054_1_NA19238_3.5_2.35bpunmasked.sam
P2 = GSM453868_yale_090121_YOAV_FC310E0_5_NA19238_2.5unmasked.sam

all: $(TARGETS)
#we MUST set this rule to keep all intermediate files!!!
.SECONDARY:

%.quality: %.unstrip
	awk '{if($$5 !=0) print $$0}' $< > $@
%.unstrip: %
	./unstripSam $^ -b > $@~ && mv $@~ $@
$(REFER):
	./haploid2diploid hg18.fa $(HAPMAP) > $(REFER).big
	./stripDiploid $(REFER).big 35 > $(REFER)
snpsInfo: $(REFER) $(P1).quality $(P2).quality
	./alnSNPsInfo $(P1).quality $(REFER) $(HAPMAP) $(EXON) -mintotal$(MINTOTAL) $(ISSAM)  > $(P1).snpsInfo~ && mv $(P1).snpsInfo~ $(P1).snpsInfo
	./alnSNPsInfo $(P2).quality $(REFER) $(HAPMAP) $(EXON) -mintotal$(MINTOTAL) $(ISSAM)  > $(P2).snpsInfo~ && mv $(P2).snpsInfo~ $(P2).snpsInfo
readsInfo: snpsInfo
	./readsAllele $(P1).quality $(P1).snpsInfo $(PLANB) $(ISSAM) > $(P1).allele~ && mv $(P1).allele~ $(P1).allele
	./readsAllele $(P2).quality $(P2).snpsInfo $(PLANB) $(ISSAM) > $(P2).allele~ && mv $(P2).allele~ $(P2).allele
showRatio: readsInfo
	grep -v isNone $(P1).allele | wc -l
	grep isRef $(P1).allele | wc -l
	grep isNonRef $(P1).allele | wc -l
	grep isMixed $(P1).allele | wc -l
	grep -v isNone $(P2).allele | wc -l
	grep isRef $(P2).allele | wc -l
	grep isNonRef $(P2).allele | wc -l
	grep isMixed $(P2).allele | wc -l


$(REFER).fai: $(REFER)
	samtools faidx $^
%.bam: %.bam.input $(REFER).fai
	samtools view -bt $(REFER).fai $< > $@~  &&  mv $@~ $@
%.sorted.bam: %.bam
	samtools sort $^ $(patsubst %.bam,%.tmp,$@) && mv $(patsubst %.bam,%.tmp.bam,$@) $@
%.pileup.snp: %.pileup.my
	awk '{if ($$6+$$7+$$8+$$9>$$10) print $$0}' $^ > $@~ && mv $@~ $@ 
%.sam: $(REFER) %.sai %.fq
	./bwa samse $^ > $@~ && mv $@~ $@
%.sai: %.fq $(REFER) $(REFER).bwt
	./bwa aln $(REFER) $<  > $@~ && mv $@~ $@
%.bwt: %
	./bwa index -a bwtsw $<

maskSNP: maskSNP.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
pileup: pileup.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
unstripSam: unstripSam.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
readsAllele: readsAllele.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
alnSNPsInfo: alnSNPsInfo.cpp CLineFields.cpp
	g++ -Wall -Werror -g -o $@ $^
fastakit: fastakit.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
compareSamTrue: compareSamTrue.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
stripDiploid: stripDiploid.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
generateReads: generateReads.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^
haploid2diploid: haploid2diploid.cpp CLineFields.cpp
	g++ -Wall -Werror -o $@ $^ 

clean:
	rm -rf $(TARGETS) *.o *~
